package com.qyhl.shop.shop.coupon.adv;

import com.google.gson.Gson;
import com.qyhl.shop.common.ShopUrl;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.base.BaseStringBean;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.shop.ShopAdvDetailBean;

public class ShopCouponAdvModel implements ShopCouponAdvContract.ShopCouponAdvModel {
    private ShopCouponAdvPresenter mPresenter;

    public ShopCouponAdvModel(ShopCouponAdvPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public void getDetail(int advId,boolean isAdv) {
        if(isAdv){
            EasyHttp.post(ShopUrl.SHOP_ADV_DETAIL)
                    .params("id", advId + "")
                    .execute(new SimpleCallBack<ShopAdvDetailBean>() {
                        @Override
                        public void onError(ApiException e) {
                            mPresenter.setError("获取广告详情失败！");
                        }

                        @Override
                        public void onSuccess(ShopAdvDetailBean bean) {
                            mPresenter.setDetail(bean);
                        }
                    });
        }else{
            EasyHttp.post(ShopUrl.SHOP_COUPON_DETAIL)
                    .params("couponId", advId + "")
                    .execute(new SimpleCallBack<ShopAdvDetailBean>() {
                        @Override
                        public void onError(ApiException e) {
                            mPresenter.setError("获取优惠券详情失败！");
                        }

                        @Override
                        public void onSuccess(ShopAdvDetailBean bean) {
                            mPresenter.setDetail(bean);
                        }
                    });
        }

    }

    @Override
    public void getCoupon(String phoneNum, int couponId, String sign) {
        EasyHttp.post(ShopUrl.SHOP_COUPON_GET)
                .params("siteId", CommonUtils.getInstance().getSite() + "")
                .params("phoneNum", phoneNum)
                .params("couponId", couponId + "")
                .params("sign", sign)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.getCouponResult(e.getMsg());
                    }

                    @Override
                    public void onSuccess(String str) {
                        BaseStringBean response = new Gson().fromJson(str, BaseStringBean.class);
                        mPresenter.getCouponResult(response.getMsg());
                    }
                });
    }
}
